limsSample.processListPage = {

    getSampleGrid: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-sampleprocess-sample-list-grid',
            // url: limsSample.batchSampleUrl + '/select/batchSampleInfos',
            url: limsSample.batchSampleProductUrl + '/batch-sample-products',
            requestData: {
                createdByOrgId_EQ: workspace.user.ext$.loginorgid
            },
            columns: limsSample.getSampleProcessSampleListColumnsNew(),
            page: true,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.refreshProcess();
                }
            },
            onRowActive: function (index, row) {
                _this.refreshProcess(row);
            },
            toolbar: [{
                type: 'button',
                text: 'T_LIMS_SAMPLE.SAMPLECODE',
                icon: 'select',
                onClick: function () {
                    var mainGrid = Gikam.getComp('lims-sampleprocess-sample-list-grid')
                    var model = Gikam.create('modal', {
                        title: 'T_CORE_CHART.SEARCHRULE',
                        height: '300',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            Gikam.postText(limsSample.batchSampleProductUrl + '/batch-sample-products', Gikam.getJsonWrapper({
                                f: {
                                    sampleCode_IN: data.sampleCode.replace(/[(\r\n)|(\ +)|(，)|(；)|(;)]/g, ',')
                                },
                                n: -1
                            })).done(function (data) {
                                console.log('data')
                                data = JSON.parse(data)
                                mainGrid.loadData(data.rows)
                            })
                        },
                    })
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'insert-fileboxnumber-form',
                                fields: [{
                                    field: 'sampleCode',
                                    title: 'T_LIMS_SAMPLE.SAMPLECODE',
                                    type: 'textarea',
                                    colspan: 2,
                                    height: 180,
                                }],
                                onLoadSuccess: function () {
                                },
                                columns: 1,
                                titleWidth: 120
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    icon: 'refresh',
                                    onClick: function () {
                                        var form = Gikam.getComp('insert-fileboxnumber-form')
                                        Gikam.getLastModal().close(form.getData())
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'cancel',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            }],
            onCellClick: function (field, row) {
                if (field === 'batchCode' && row.batchId) {
                    Gikam.loadBatchDetailPageV12(row.batchId, true)
                } else if (field === 'sampleCode' && row.id) {
                    if (row.ext$.type === 'TJ' || row.batchCode === 'QC001') {
                        _this.loadSampleTestTaskDetailPage(row)
                    } else if (row.ext$.type === 'PJ') {
                        _this.loadPjSampleTaskDetailPage(row)
                    }
                }
            },
        }
    },

    loadPjSampleTaskDetailPage: function (row) {
        workspace.window.load(IFM_CONTEXT + '/secure/basemodule/business/order-tasks/page/pj-sample-test-task-detail' + Gikam.param({
            batchSampleId_EQ: row.batchSampleId,
            productId_EQ: row.productId
        }))
    },

    loadSampleTestTaskDetailPage: function (row) {
        workspace.window.load(IFM_CONTEXT + '/secure/basemodule/business/order-tasks/page/sample-test-task-detail' + Gikam.param({
            batchSampleId: row.batchSampleId,
            productId: row.productId
        }))
    },

    refreshProcess: function (row) {
        console.log(row)
        const iframeDom = top.document.querySelector("#lims-sampleprocess-process-iframe >iframe");
        console.log(iframeDom)
        let flag = false  //是否初次加载
        iframeDom.onload = function () {
            flag = true
            iframeDom.contentWindow.postMessage({
                sampleId: row.id
            }, "*");
        }
        if (!flag) {
            iframeDom.contentWindow.postMessage({
                sampleId: row.id
            }, "*");
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-sampleprocess-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                title: 'BASEMODULE.SAMPLE.TAB.LIMS-SAMPLEPROCESS-LIST',
                items: [_this.getSampleGrid()]
            },
            east: {
                title: 'BASEMODULE.SAMPLE.TAB.LIMS-SAMPLEPROCESS-PROCESS-IFRAME',
                width: '65%',
                items: [{
                    type: 'iframe',
                    src: '/static/basemodule/samplemanage/sample-process/index.html',
                    id: 'lims-sampleprocess-process-iframe',
                    // scrolling: 'yes'
                }]
            }
        });
    },

    init: function () {
        this.param = {};
        this.create();
    }
};
